package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.entitiy.TbTest;
import com.ljc.cjgl.entitiy.TestRegister;
import com.ljc.cjgl.form.TestRegisterForm;
import com.ljc.cjgl.service.TestRegisterService;

@Controller
@RequestMapping(value="/calculate")
public class CalculateController {
	@Autowired
	private TestRegisterService service;

	@RequestMapping(value="manage")
	public String manage(ModelMap map) {
		List<TbTest> tests=service.findDistinctTest();
		List<TbClass> classes=service.findDistinctStudentTbClass();
		map.put("tests", tests);
		map.put("classes", classes);
		return "calculate/manage";
	}
	
	private Specification<TestRegister> bulidSpec(TestRegisterForm form){
		//boolean flag1=true;
		//boolean flag2=true;
		HashSet<Rule> rules=new HashSet<>();
		Integer classId = form.getClassId();
		Integer testId = form.getTestId();
		String search = form.getSearch();
		if(classId!=null) {
			//flag1=false;
			rules.add(Rule.property("student.tbClass.id").equal(classId));
		}
		if(testId!=null) {
			//flag2=false;
			rules.add(Rule.property("test.id").equal(testId));
		}
		/*if(flag1||flag2) {
			rules.add(Rule.property("id").isNull());
		}*/
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("student.tbClass.name").contain(search));
			rules2.add(Rule.property("test.name").contain(search));
			rules2.add(Rule.property("student.name").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageable,TestRegisterForm form) {
		Integer classId = form.getClassId();
		Integer testId=form.getTestId();
		if(classId!=null&&testId!=null) {
			service.calculate(classId,testId);
		}
		PageRequest pageRequest = pageable.bulidPageRequest();
		Specification<TestRegister> specification = bulidSpec(form);
		Page<TestRegister> page=service.findAll(specification, pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping(value="/gradeChange")
	@ResponseBody
	public Object gradeChange(Integer id,Integer grade) {
		service.gradeChange(id,grade);
		return TableResult.bulidResult(true, "数据修改成功");
	}
	
}
